﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.TaskService;
using MobileSales.Model.Tasks;
using MobileSales.Website.Infrastructure;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class SchedulesController : ControllerBase
    {
        #region Constants
        public const string ScheduleDateSessionKey = "schedules_selectedDate"; 
        #endregion

        #region Index
        //
        // GET: /Schedules/
        public ActionResult Index(int? employeeId, DateTime? date)
        {
            if (date.HasValue)
                Session[ScheduleDateSessionKey] = date.Value;
            else
                Session[ScheduleDateSessionKey] = DateTime.Now;


            if (employeeId.HasValue)
            {
                IEnumerable<Schedule> schedules = null;
                try
                {
                    TaskServiceClient taskService = new TaskServiceClient();
                    taskService.AddValidationToken(this.Token);
                    schedules = taskService.GetScheduleList(employeeId.Value);
                    taskService.Close();
                }
                catch (Exception ex)
                {
                    CLogger.LogDebug(ex);
                    this.SetMessage(ErrorStrings.ErrorScheduleList, MessageType.Error);
                }
                ViewData["employeeId"] = employeeId;
                return View(schedules);
            }
            else
            {
                return View();
            }
        } 
        #endregion

        #region Details
        //
        // GET: /Schedules/Details/5

        public ActionResult Details(int employeeId, DateTime date)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                Schedule schedule = client.GetScheduleByDate(employeeId,date);
                client.Close();
                return View(schedule);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.ScheduleNotFound, MessageType.Error);
                return RedirectToAction("Index", new { employeeId = employeeId });
            }
        } 
        #endregion
    }
}
